/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.prodcuct.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.anny.prodcuct.entity.ProductDetail;
import com.jeeplus.modules.anny.prodcuct.mapper.ProductDetailMapper;

/**
 * 商品表(小)  用于商品细节规格描述具体价格以及库存Service
 * @author zhengcq
 * @version 2018-10-17
 */
@Service
@Transactional(readOnly = true)
public class ProductDetailService extends CrudService<ProductDetailMapper, ProductDetail> {

	public ProductDetail get(String id) {
		return super.get(id);
	}
	
	public List<ProductDetail> findList(ProductDetail productDetail) {
		return super.findList(productDetail);
	}
	
	public Page<ProductDetail> findPage(Page<ProductDetail> page, ProductDetail productDetail) {
		return super.findPage(page, productDetail);
	}


	/**
	 * 如果库里存在规格相同的商品则覆盖之前的。更新最新的。
	 * @param productDetail
	 */
	@Transactional(readOnly = false)
	public void save(ProductDetail productDetail) {
		ProductDetail p = new ProductDetail();
		p.setProductSize(productDetail.getProductSize());
		p.setProductId(productDetail.getProductId());
		List<ProductDetail> list = this.findList(p);
		if (productDetail.getIsNewRecord()){
			if (list != null && list.size() > 0){
				for (ProductDetail pro : list){
					super.delete(pro);
				}
			}
			productDetail.preInsert();
			mapper.insert(productDetail);
		}else{
			if (list != null && list.size() > 0){
				for (ProductDetail pro : list){
					if (productDetail.getId().equals(pro.getId()) ){
						continue;
					}
					super.delete(pro);
				}
			}
			productDetail.preUpdate();
			mapper.update(productDetail);
		}



	}
	
	@Transactional(readOnly = false)
	public void delete(ProductDetail productDetail) {
		super.delete(productDetail);
	}
	
	/**
	 * 根据商品删除规格
	 * @param id
	 */
	@Transactional(readOnly = false)
	public void deleteByLogicProductId(String id){
		mapper.deleteByLogicProductId(id);
	}
}